package com.buchong.controller;


import com.buchong.pojo.Book;
import com.buchong.pojo.Comment;
import com.buchong.pojo.Order_Demo;
import com.buchong.pojo.Order_item;
import com.buchong.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
public class Order_DemoController {
    @Autowired
    private Order_De_Service order_de_service;
    @Autowired
    private PayService payService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private BookService bookService;

    @RequestMapping("/show")
    public String show2(@RequestParam(defaultValue = "1", required = false) Integer uid, Model model, @RequestParam(defaultValue = "100", required = false) Integer id, HttpSession session) {
        session.setAttribute("id", uid);
        List<Order_Demo> order_demos = new ArrayList<>();
        switch (id) {
            case 100:
                order_demos = show4(uid, -100, 0);
                break;
            case 200:
                order_demos = show4(uid, payService.fingByName("未支付").getId(), 0);
                break;
            case 300:
                order_demos = show4(uid, payService.fingByName("已支付").getId(), 0);
                break;
            case 400:
                order_demos = show4(uid, payService.fingByName("已发货").getId(), 0);
                break;
            case 500:
                order_demos = show4(uid, payService.fingByName("已收货").getId(), 0);
                break;
            default:
                order_demos = show4(uid, -100, 0);
                break;
        }
        model.addAttribute("order", order_demos);
        model.addAttribute("index", id);
        return "待付款";
    }

    /**
     * 付款成功候得函数
     */
    @RequestMapping("/success")
    public String success(@RequestParam("id") String id, Model model, HttpSession session) {
        Integer uid = (Integer) session.getAttribute("uid");
        uid = 1;
        Order_Demo order_demo = new Order_Demo();
        order_demo.setId(Integer.parseInt(id));
        order_demo.setPayTime(LocalDate.now() + " " + LocalTime.now().getHour() + ":" + LocalTime.now().getMinute() + ":" + LocalTime.now().getSecond());
        order_demo.setOrderStatus(payService.fingByName("已支付").getId() + "");
        order_de_service.upOne(order_demo);
        List<Order_Demo> order_demos = show4(uid, payService.fingByName("已支付").getId(), 0);
        model.addAttribute("order", order_demos);
        model.addAttribute("index", 300);
        return "待付款";
    }

    /***
     *
     * @param id 用户id
     * @return
     */
    public List<Order_Demo> show4(Integer id, Integer type, Integer cid) {

        List<Order_Demo> order_demos = new ArrayList<>();
        if (type == -100) {
            order_demos = order_de_service.find(id);
        } else {
            order_demos = order_de_service.findAll(id, type);
        }

        if (order_demos.size() != 0) {
            for (int i = 0; i < order_demos.size(); i++) {
                order_demos.get(i).setOrder_items(orderItemService.find(order_demos.get(i).getId(), cid));
                order_demos.get(i).setOrderStatus(payService.findById(Integer.parseInt(order_demos.get(i).getOrderStatus())).getName());
                for (int j = 0; j < order_demos.get(i).getOrder_items().size(); j++) {
                    order_demos.get(i).getOrder_items().get(j).setBook(bookService.findOne(order_demos.get(i).getOrder_items().get(j).getBookId()));
                }
            }
        }
        return order_demos;
    }

    /**
     * @param id    订单id
     * @param model
     * @param
     * @return
     */
    @RequestMapping("del")
    public String show5(Integer id, Model model, HttpSession session) {
        Integer uid = (Integer) session.getAttribute("uid");
        uid=1;
        order_de_service.delOne(id);
        List<Order_Demo> order_demos = show4(uid, payService.fingByName("未支付").getId(), 0);
        model.addAttribute("order", order_demos);
        model.addAttribute("index", 200);
        return "待付款";
    }

    @RequestMapping("/up")
    public String show6(@RequestParam("id") Integer id, Model model, HttpSession session) {
        Integer uid = (Integer) session.getAttribute("uid");
        Order_Demo order_demo = new Order_Demo();
        order_demo.setId(id);
        order_demo.setFinishTime(LocalDate.now() + " " + LocalTime.now().getHour() + ":" + LocalTime.now().getMinute() + ":" + LocalTime.now().getSecond());
        order_demo.setOrderStatus(payService.fingByName("已收货").getId() + "");
        order_de_service.upOne(order_demo);
        List<Order_Demo> order_demos = show4(uid, payService.fingByName("已收货").getId(), 0);
        model.addAttribute("order", order_demos);
        model.addAttribute("index", 500);
        return "待付款";
    }

    @RequestMapping("/common")
    public String show7(Model model, @RequestParam(required = false, defaultValue = "100") Integer index, HttpSession session) {
        Integer uid = (Integer) session.getAttribute("uid");
        uid = 1;
        List<Order_Demo> order_demos;
        switch (index) {
            case 100:
                order_demos = show4(uid, payService.fingByName("已收货").getId(), 0);
                break;
            case 200:
                order_demos = show4(uid, payService.fingByName("已收货").getId(), 1);
                break;
            default:
                order_demos = show4(uid, payService.fingByName("已收货").getId(), 0);

        }
        model.addAttribute("order", order_demos);
        model.addAttribute("index", index);
        return "评价";
    }


    @RequestMapping("/com")
    public String show8(HttpSession session, Model model, @RequestParam("common") String common, @RequestParam("itemid") Integer itemid, @RequestParam("bookid") Integer bookid) {
        Integer uid = (Integer) session.getAttribute("uid");
        uid = 1;
        Comment comment = new Comment();
        comment.setUserId(uid);
        comment.setOrderItemId(itemid);
        comment.setCreateTime(LocalDate.now() + " " + LocalTime.now().getHour() + ":" + LocalTime.now().getMinute() + ":" + LocalTime.now().getSecond());
        comment.setBookId(bookid);
        comment.setComment(common);
        commentService.addOne(comment);
        Order_item order_item = new Order_item();
        order_item.setId(itemid);
        order_item.setCommentId(1);
        orderItemService.up(order_item);
        List<Order_Demo> order_demos = show4(uid, payService.fingByName("已收货").getId(), 1);
        model.addAttribute("order", order_demos);
        model.addAttribute("index", 200);
        return "评价";
    }

    @RequestMapping("/kk")
    public String show9(Integer id, Model model) {
        Book book = bookService.findOne(id);
        Order_Demo order_demo = new Order_Demo();
        order_demo.setUserId(1);
       String time= LocalDate.now() + " " + LocalTime.now().getHour() + ":" + LocalTime.now().getMinute() + ":" + LocalTime.now().getSecond();
        order_demo.setCreateTime(time);
        order_demo.setOrderNum(book.getInfo());
        order_demo.setFinishTime("");
        order_demo.setPayTime("");
        order_demo.setCostPrice(0.0);
        order_demo.setPressPrice(1.5);
        order_demo.setSendTime("");
        order_demo.setReducePrice(0.0);
        order_demo.setTotalPrice(book.getPrice());
        order_demo.setShoww("true");
        order_demo.getSendTime();
        order_demo.setUserMessage("");
        order_demo.setAddrId(1);
        order_demo.setOrderStatus("4");
        order_de_service.add(order_demo);


        Order_item order_item = new Order_item();
        order_item.setBookId(book.getId());
        order_item.setOrderId(order_de_service.sel(time).getId());
        order_item.setCommentId(0);
        order_item.setNewPrice((int) book.getPrice());
        order_item.setNum(1);
        order_item.setOldPrice((int) book.getPrice());
        order_demo.setCreateTime(LocalDate.now() + " " + LocalTime.now().getHour() + ":" + LocalTime.now().getMinute() + ":" + LocalTime.now().getSecond());
        orderItemService.add(order_item);
        List<Order_Demo> order_demos = show4(1, -100, 0);
        model.addAttribute("order", order_demos);
        model.addAttribute("index", 100);
        return "待付款";
    }
}
